﻿<!doctype html>
<html>
<head>
    <title>SpreadJS - Basic Search</title>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    
    <link href="../../../external/spreadjs/css/gcspread.sheets.excel2013white.9.40.20161.0.css" rel="stylesheet" type="text/css" />

    <script src="../../../external/external/jquery-1.8.2.min.js" type="text/javascript"></script>

    <script type="text/javascript" src="../../../external/spreadjs/gcspread.sheets.all.9.40.20161.0.min.js"></script>

    <style type="text/css">
    /*css_begin*/
        label {
            display:inline-block;
            width: 80px;
            margin-right: 12px;
            text-align:right;
        }

        input[type="checkbox"] {
            margin-left: 20px;
        }

        input[type="checkbox"] + label {
            width: auto;
            text-align:left;
        }

        input[type="text"] {
            width: 100px;
        }

        select {
            width: 104px;
        }

        input[type="button"] {
            width: 104px;
        }
    /*css_end*/
    </style>

    <script id="scriptInit" type="text/javascript">
    /*code_begin*/
    var spreadNS = GcSpread.Sheets, spread;

    $(document).ready(function () {
        spread = new GcSpread.Sheets.Spread(document.getElementById("ss"), { sheetCount: 3 });
        initSpread(spread);
    });

    function initSpread(spread) {
        $("#btnFindNext").click(function () {
            var sheet = spread.getActiveSheet();
            var searchCondition = getSearchCondition();
            var within = $("#searchWithin").prop("value");
            var searchResult = null;
            if (within == "sheet") {
                var sels = sheet.getSelections();
                if (sels.length > 1) {
                    searchCondition.searchFlags |= spreadNS.SearchFlags.BlockRange;
                } else if (sels.length == 1) {
                    var spanInfo = getSpanInfo(sheet, sels[0].row, sels[0].col);
                    if (sels[0].rowCount != spanInfo.rowSpan && sels[0].colCount != spanInfo.colSpan) {
                        searchCondition.searchFlags |= spreadNS.SearchFlags.BlockRange;
                    }
                }
                searchResult = getResultSearchinSheetEnd(searchCondition);
                if (searchResult == null || searchResult.searchFoundFlag == spreadNS.SearchFoundFlags.None) {
                    searchResult = getResultSearchinSheetBefore(searchCondition);
                }
            } else if (within == "workbook") {
                searchResult = getResultSearchinSheetEnd(searchCondition);
                if (searchResult == null || searchResult.searchFoundFlag == spreadNS.SearchFoundFlags.None) {
                    searchResult = getResultSearchinWorkbookEnd(searchCondition);
                }
                if (searchResult == null || searchResult.searchFoundFlag == spreadNS.SearchFoundFlags.None) {
                    searchResult = getResultSearchinWorkbookBefore(searchCondition);
                }
                if (searchResult == null || searchResult.searchFoundFlag == spreadNS.SearchFoundFlags.None) {
                    searchResult = getResultSearchinSheetBefore(searchCondition);
                }
            }

            if (searchResult != null && searchResult.searchFoundFlag != spreadNS.SearchFoundFlags.None) {
                spread.setActiveSheetIndex(searchResult.foundSheetIndex);
                var sheet = spread.getActiveSheet();
                sheet.setActiveCell(searchResult.foundRowIndex, searchResult.foundColumnIndex);
                if ((searchCondition.searchFlags & spreadNS.SearchFlags.BlockRange) == 0) {
                    sheet._clearSelectionImp();
                    sheet.addSelection(searchResult.foundRowIndex, searchResult.foundColumnIndex, 1, 1);
                }
                //scrolling
                if (searchResult.foundRowIndex < sheet.getViewportTopRow(1)
                        || searchResult.foundRowIndex > sheet.getViewportBottomRow(1)
                        || searchResult.foundColumnIndex < sheet.getViewportLeftColumn(1)
                        || searchResult.foundColumnIndex > sheet.getViewportRightColumn(1)
                        ) {
                    sheet.showCell(searchResult.foundRowIndex,
                            searchResult.foundColumnIndex,
                            spreadNS.VerticalPosition.center,
                            spreadNS.HorizontalPosition.center);
                } else {
                    sheet.repaint();
                }
            } else {
                //Not Found
                $("#searchNotFoundDialog").dialog("open");
            }
        });
    };

    function getSpanInfo(sheet, row, col) {
        var span = sheet.getSpans(new spreadNS.Range(row, col, 1, 1));
        if (span.length > 0) {
            return {rowSpan:span[0].rowCount, colSpan:span[0].colCount};
        } else {
            return {rowSpan:1, colSpan:1};
        }
    }

    function getResultSearchinSheetEnd(searchCondition) {
        var sheet = spread.getActiveSheet();
        searchCondition.startSheetIndex = spread.getActiveSheetIndex();
        searchCondition.endSheetIndex = spread.getActiveSheetIndex();

        if (searchCondition.searchOrder == spreadNS.SearchOrder.ZOrder) {
            searchCondition.findBeginRow = sheet._activeRowIndex;
            searchCondition.findBeginColumn = sheet._activeColIndex + 1;
        } else if (searchCondition.searchOrder == spreadNS.SearchOrder.NOrder) {
            searchCondition.findBeginRow = sheet._activeRowIndex + 1;
            searchCondition.findBeginColumn = sheet._activeColIndex;
        }

        if ((searchCondition.searchFlags & spreadNS.SearchFlags.BlockRange) > 0) {
            var sel = sheet.getSelections()[0];
            searchCondition.rowStart = sel.row;
            searchCondition.columnStart = sel.col;
            searchCondition.rowEnd = sel.row + sel.rowCount - 1;
            searchCondition.columnEnd = sel.col + sel.colCount - 1;
        }
        var searchResult = spread.search(searchCondition);
        return searchResult;
    }

    function getResultSearchinSheetBefore(searchCondition) {
        var sheet = spread.getActiveSheet();
        searchCondition.startSheetIndex = spread.getActiveSheetIndex();
        searchCondition.endSheetIndex = spread.getActiveSheetIndex();
        if ((searchCondition.searchFlags & spreadNS.SearchFlags.BlockRange) > 0) {
            var sel = sheet.getSelections()[0];
            searchCondition.rowStart = sel.row;
            searchCondition.columnStart = sel.col;
            searchCondition.findBeginRow = sel.row;
            searchCondition.findBeginColumn = sel.col;
            searchCondition.rowEnd = sel.row + sel.rowCount - 1;
            searchCondition.columnEnd = sel.col + sel.colCount - 1;
        } else {
            searchCondition.rowStart = -1;
            searchCondition.columnStart = -1;
            searchCondition.findBeginRow = -1;
            searchCondition.findBeginColumn = -1;
            searchCondition.rowEnd = sheet._activeRowIndex;
            searchCondition.columnEnd = sheet._activeColIndex;
        }

        var searchResult = spread.search(searchCondition);
        return searchResult;
    }

    function getResultSearchinWorkbookEnd(searchCondition) {
        searchCondition.rowStart = -1;
        searchCondition.columnStart = -1;
        searchCondition.findBeginRow = -1;
        searchCondition.findBeginColumn = -1;
        searchCondition.rowEnd = -1;
        searchCondition.columnEnd = -1;
        searchCondition.startSheetIndex = spread.getActiveSheetIndex() + 1;
        searchCondition.endSheetIndex = -1;
        var searchResult = spread.search(searchCondition);
        return searchResult;
    }

    function getResultSearchinWorkbookBefore(searchCondition) {
        searchCondition.rowStart = -1;
        searchCondition.columnStart = -1;
        searchCondition.findBeginRow = -1;
        searchCondition.findBeginColumn = -1;
        searchCondition.rowEnd = -1;
        searchCondition.columnEnd = -1;
        searchCondition.startSheetIndex = -1
        searchCondition.endSheetIndex = spread.getActiveSheetIndex() - 1;
        var searchResult = spread.search(searchCondition);
        return searchResult;
    }

    function getSearchCondition() {
        var searchCondition = new spreadNS.SearchCondition();
        var findWhat = $("#txtSearchWhat").prop("value");
        var within = $("#searchWithin").prop("value");
        var order = $("#searchOrder").prop("value");
        var lookin = $("#searchLookin").prop("value");
        var matchCase = $("#chkSearchMachCase").prop("checked");
        var matchEntire = $("#chkSearchMachEntire").prop("checked");
        var useWildCards = $("#chkSearchUseWildCards").prop("checked");

        searchCondition.searchString = findWhat;
        if (within == "sheet") {
            searchCondition.startSheetIndex = spread.getActiveSheetIndex();
            searchCondition.endSheetIndex = spread.getActiveSheetIndex();
        }
        if (order == "norder") {
            searchCondition.searchOrder = spreadNS.SearchOrder.NOrder;
        } else {
            searchCondition.searchOrder = spreadNS.SearchOrder.ZOrder;
        }
        if (lookin == "formula") {
            searchCondition.searchTarget = spreadNS.SearchFoundFlags.CellFormula;
        } else {
            searchCondition.searchTarget = spreadNS.SearchFoundFlags.CellText;
        }

        if (!matchCase) {
            searchCondition.searchFlags |= spreadNS.SearchFlags.IgnoreCase;
        }
        if (matchEntire) {
            searchCondition.searchFlags |= spreadNS.SearchFlags.ExactMatch;
        }
        if (useWildCards) {
            searchCondition.searchFlags |= spreadNS.SearchFlags.UseWildCards;
        }

        return searchCondition;
    }
    /*code_end*/
    </script>

</head>
<body>
<div class="sample-turtorial">
    <div id="ss" style="width:100%; height:340px;border: 1px solid gray;"></div>
    <div class="demo-options">
        <div class="option-row">
            <label>Find what:</label>
            <input id="txtSearchWhat" type="text" />
        </div>
        <div class="option-row">
            <label>Within:</label>
            <select id="searchWithin">
                <option value="sheet" selected>Sheet</option>
                <option value="workbook">Workbook</option>
            </select>
            <input id="chkSearchMachCase" type="checkbox" />
            <label for="chkSearchMachCase">Match case</label>
        </div>
        <div class="option-row">
            <label>Look in:</label>
            <select id="searchLookin">
                <option value="value" selected>Values</option>
                <option value="formula">Formulas</option>
            </select>
            <input id="chkSearchMachEntire" type="checkbox" />
            <label for="chkSearchMachEntire">Match exactly</label>
        </div>
        <div class="option-row">
            <label>Search:</label>
            <select id="searchOrder">
                <option value="zorder" selected>By Rows</option>
                <option value="norder">By Columns</option>
            </select>
            <input id="chkSearchUseWildCards" type="checkbox" />
            <label for="chkSearchUseWildCards">Use wildcards</label>
        </div>
        <div class="option-row">
            <label></label>
            <input id="btnFindNext" type="button" value="Find Next" />
        </div>
    </div>
</div>
</body>
</html>
